package sitesettinghandler

import (
	"github.com/gin-gonic/gin"
	"time"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/devploys"
	"yundian/internel/app/models/devrule"
	"yundian/internel/app/models/ploys"
	"yundian/internel/app/models/prices"
	"yundian/internel/pkg/dingo"
	. "yundian/internel/pkg/dingo/result"
	"yundian/internel/pkg/utils"
)

//计费规则列表
func BillingRulesList(c *gin.Context) {
	param := &struct {
		UserName    string `json:"userName" form:"userName" `
		SelectTime  string `json:"selectTime" form:"selectTime"`
		BaseListReq request.BaseListReq
	}{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()
	//获取数据
	data := dao.NewSiteSettingDao().BillingRulesListDao(&param.BaseListReq)
	//错误处理
	if data.Err != nil {
		Response(c)("", CodeInternalServerError, data.Data)(ServerError)
	}

	Response(c)("", CodeSuccess, ResponseList{
		PageNo:     param.BaseListReq.PageNo,
		PageSize:   param.BaseListReq.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  0,
		Data:       data.Unwrap(),
	})(OK)
}

//计费规则修改
func BillingRulesEdit(c *gin.Context) {
	param := &devrule.DeviceRulesModel{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()
	param.UpdateAt = dingo.Time(time.Now().Unix())
	updateVal := map[string]interface{}{
		"status":     param.Status,
		"base_price": param.BasePrice,
		"base_times": param.BaseTimes,
		//"create_at":          dingo.Time(param.CreateAt),
		"deposit":            param.Deposit,
		"free_times":         param.FreeTimes,
		"is_recommend":       param.IsRecommend,
		"max_pay_price":      param.MaxPayPrice,
		"max_time":           param.MaxTime,
		"pay_interval":       param.PayInterval,
		"pay_interval_price": param.PayIntervalPrice,
		"pay_money":          param.PayMoney,
		"pay_tactics":        param.PayTactics,
		"priority":           param.Priority,
		"rule_name":          param.RuleName,
		"name":               param.Name,
		"set_meal_num":       param.SetMealNum,
		"type_id":            param.TypeId,
		"update_at":          param.UpdateAt,
		"use_duration":       param.UseDuration,
	}
	Response(c)("修改成功", CodeUpdateSuccess, dao.NewSiteSettingDao().BillingRulesEditDao(param.Id, updateVal).Unwrap())(OK)
}

//计费规则新增
func BillingRulesSave(c *gin.Context) {
	param := &devrule.DeviceRulesModel{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()

	param.UpdateAt = dingo.Time(time.Now().Unix())
	param.CreateAt = dingo.Time(time.Now().Unix())
	Response(c)("新增成功", CodeUpdateSuccess, dao.NewSiteSettingDao().BillingRulesSaveDao(param).Unwrap())(OK)
}

//计费规则删除
func BillingRulesDelete(c *gin.Context) {
	param := &struct {
		Id int `json:"id"`
	}{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()
	Response(c)("删除成功", CodeUpdateSuccess, dao.NewSiteSettingDao().BillingRulesDeleteDao(param.Id).Unwrap())(OK)
}

//应用计费规则
func BillingRulesUse(c *gin.Context) {
	param := &struct {
		Id       int `json:"id"`
		PricesId int `json:"pricesId"`
	}{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()
	//查询计费规则模板
	deviceRulesInfo := dao.NewSiteSettingDao().GetByIdBillingRulesDao(param.Id).Unwrap().(devrule.DeviceRulesModel)
	m := prices.New()
	m.Name = deviceRulesInfo.Name
	m.DeviceTypeId = deviceRulesInfo.TypeId
	m.PlaceId = deviceRulesInfo.Id
	m.Status = deviceRulesInfo.Status
	m.ProductId = utils.Str2Md5(helper.GenerateUUID())

	// -----套餐模式 开始------
	m.UseDuration = deviceRulesInfo.UseDuration // 使用时长
	m.PayMoney = deviceRulesInfo.PayMoney       // 支付金额-单位分
	m.SetMealNum = deviceRulesInfo.SetMealNum   // 规则序号
	m.RuleName = deviceRulesInfo.RuleName       // 规则名
	m.IsRecommend = deviceRulesInfo.IsRecommend // 是否推荐
	m.Priority = deviceRulesInfo.Priority       // 排序
	// -----套餐模式 结束------

	// -----计时模式 开始------
	m.MaxPayPrice = deviceRulesInfo.MaxPayPrice           // 日封顶价格
	m.PayIntervalPrice = deviceRulesInfo.PayIntervalPrice // 每60分钟支付2元
	m.PayInterval = int(deviceRulesInfo.PayInterval)      // 每60分钟支付2元
	m.MaxTime = deviceRulesInfo.MaxTime                   // 最大使用时长
	m.BasePrice = deviceRulesInfo.BasePrice               // 最低使用价格
	m.BaseTimes = int(deviceRulesInfo.BaseTimes)          // 最低使用时长
	m.FreeTimes = int(deviceRulesInfo.FreeTimes)          // 免费使用时长
	m.Deposit = deviceRulesInfo.Deposit                   //  押金
	// -----计时模式 结束------

	m.CreateAt = dingo.Time(time.Now().Unix())
	Response(c)("应用成功", CodeUpdateSuccess, dao.NewSiteSettingDao().BillingRulesUseDao(param.PricesId, m).Unwrap())(OK)

}

//奖励规则列表
func RewardPloyList(c *gin.Context) {
	param := &struct {
		BaseListReq request.BaseListReq
	}{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()
	//获取数据
	data := dao.NewSiteSettingDao().RewardPloyListDao(&param.BaseListReq)
	//错误处理
	if data.Err != nil {
		Response(c)("", CodeInternalServerError, data.Data)(ServerError)
	}

	Response(c)("", CodeSuccess, ResponseList{
		PageNo:     param.BaseListReq.PageNo,
		PageSize:   param.BaseListReq.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  0,
		Data:       data.Unwrap(),
	})(OK)
}

//奖励规则修改
func RewardPloyEdit(c *gin.Context) {
	param := &devploys.DevicePloysModel{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()
	param.UpdateAt = dingo.Time(time.Now().Unix())
	updateVal := map[string]interface{}{
		"id":          param.Id,
		"range":       param.Range,
		"num":         param.Num,
		"type":        param.Type,
		"status":      param.Status,
		"type_id":     param.TypeId,
		"shared_mode": param.SharedMode,
		"ploy_type":   param.PloyType,
		"ploy_name":   param.PloyName,
		"place_id":    param.PlaceId,
		"place_name":  param.PlaceName,
		"user_id":     param.UserId,
		"user_name":   param.UserName,
		"name":        param.Name,
		//"create_at":   param.CreateAt,
		"update_at": param.UpdateAt,
	}
	Response(c)("修改成功", CodeUpdateSuccess, dao.NewSiteSettingDao().RewardPloyEditDao(param.Id, updateVal).Unwrap())(OK)
}

//奖励规则保存
func RewardPloySave(c *gin.Context) {
	param := &devploys.DevicePloysModel{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()

	param.UpdateAt = dingo.Time(time.Now().Unix())
	param.CreateAt = dingo.Time(time.Now().Unix())
	Response(c)("新增成功", CodeUpdateSuccess, dao.NewSiteSettingDao().RewardPloySaveDao(param).Unwrap())(OK)
}

//奖励规则删除
func RewardPloyDelete(c *gin.Context) {
	param := &struct {
		Id int `json:"id"`
	}{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()
	Response(c)("删除成功", CodeUpdateSuccess, dao.NewSiteSettingDao().RewardPloyDeleteDao(param.Id).Unwrap())(OK)
}

//奖励规则应用
func RewardPloyUse(c *gin.Context) {
	param := &struct {
		Id      int `json:"id"`
		PloysId int `json:"ploysId"`
	}{}
	dingo.ErrorIf(c.Bind(param)).Unwrap()
	devicePloysInfo := dao.NewSiteSettingDao().GetByIdRewardPloyDao(param.Id).Unwrap().(devploys.DevicePloysModel)
	ploy := ploys.New()
	ploy.Name = devicePloysInfo.Name
	ploy.Type = devicePloysInfo.Type
	ploy.TypeId = devicePloysInfo.TypeId
	ploy.Status = devicePloysInfo.Status
	ploy.SharedMode = devicePloysInfo.SharedMode
	ploy.PloyType = devicePloysInfo.PloyType
	ploy.PloyName = devicePloysInfo.PloyName
	ploy.Num = devicePloysInfo.Num
	ploy.Range = devicePloysInfo.Range
	ploy.PlaceId = devicePloysInfo.PlaceId
	ploy.PlaceName = devicePloysInfo.PlaceName
	ploy.CreateAt = dingo.Time(time.Now().Unix())
	ploy.UpdateAt = dingo.Time(time.Now().Unix())
	Response(c)("应用成功", CodeUpdateSuccess, dao.NewSiteSettingDao().RewardPloyUseDao(param.PloysId, ploy).Unwrap())(OK)

}
